package com.patrol.controller;

import com.patrol.auth.pojo.AdminUser;
import com.patrol.service.UserImageService;
import com.patrol.utils.FileUtil;
import com.patrol.utils.Result;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

//import com.patrol.utils.FileUploadUtils;

/**
 * @author 宋灿
 * @version 1.0
 * @description: TODO
 * @date 2021/8/13 18:51
 */
@CrossOrigin
@Slf4j
@Api(value = "个人中心头像上传接口")
@RestController
@RequestMapping("/api")
public class UserImageController {
    @Resource
    private UserImageService userImageService;

    /**
     * 文件上传
     *
     * @param
     * @return
     * @throws IOException
     */
    //@ResponseBody
    @ApiOperation(value = "上传头像文件")
    @PostMapping("/user/getUserImage")
    public Result fileUpload(@RequestParam("file") MultipartFile multipartFile,
                             @RequestParam("username") String username) {

        if (StringUtil.isNullOrEmpty(username)) {
            return Result.fail("文件名不能为空！");
        }

        if (multipartFile == null) {
            return Result.fail("未选择要上传的文件");
        }

        FileUtil fu = new FileUtil();
        // 获取图片的名字, 使用UUID重命名文件
        String fileName = UUID.randomUUID().toString().replace("-", "") + "." + fu.getSuffix(multipartFile.getOriginalFilename());
        if (!multipartFile.isEmpty()) {
            //获取项目classes/static的地址
            String staticPath = ClassUtils.getDefaultClassLoader().getResource("static").getPath();
            // 图片存储目录
            String url_path = "head/";
            //图片保存路径目录
            String savePath = staticPath + "/" + url_path;
            System.out.println("图片保存地址：" + savePath + fileName);
            // 访问路径=静态资源路径+文件目录路径
            String visitPath = "static/" + url_path;
            System.out.println("图片访问uri：" + visitPath + fileName);
            // 文件夹地址 + 名字 = 图片的路径
            String fileAddress = url_path + fileName;
            try {
                // 把图片前端访问静态路径head/...写入数据库
                AdminUser adminUser = new AdminUser();
                adminUser.setImg(fileAddress);
                adminUser.setUsername(username);
                boolean b = userImageService.headerImageUpload(adminUser, staticPath);
                if (b) {
                    // 图片上传的工具类
                    // 参数一：图片的编码格式的字节数组
                    // 参数二：图片存放的文件夹地址
                    // 参数三：图片的名字
                    FileUtil.uploadFile(multipartFile.getBytes(), savePath, fileName);
                    return Result.ok();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return Result.fail();
    }
}
